package com.lg.atp.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lg.atp.entity.TblMenuButtonInfo;
import com.lg.atp.sercurity.SecurityUserHolder;
import com.lg.atp.service.IMenuService;
import com.lg.atp.vo.user.UserVo;

@RestController
public class LoginController {

	
	@Autowired
	private IMenuService iMenuService;
	
    @RequestMapping("/")
    public ModelAndView login(HttpServletRequest request){
    	
    	ModelAndView model = new ModelAndView();
    	//登录认真失败
        Exception e =  request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION") == null?null:(Exception)request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        if(e != null){
        	model.addObject("error", 1);
        	model.addObject("errMsg", e.getMessage());
        	
        }
        //权限认证失败
        AccessDeniedException ae = request.getAttribute("SPRING_SECURITY_403_EXCEPTION") == null?null:(AccessDeniedException)request.getAttribute("SPRING_SECURITY_403_EXCEPTION"); 
        if(ae != null)
        {
        	model.addObject("error", 1);
        	model.addObject("errMsg", ae.getMessage());
        }
        
        model.setViewName("index");
    	return model;
    }

    @RequestMapping("main")
    public ModelAndView toMain(HttpServletRequest request){

    	ModelAndView model = new ModelAndView();
    	UserVo user = SecurityUserHolder.getCurrentUser();
    	model.addObject("user", user);
    	model.setViewName("main");
    	List<TblMenuButtonInfo> menus = iMenuService.getUserMenu(user.getId());
    	model.addObject("menus", menus);
        return model;
    }
    
    @RequestMapping("welcome")
    public ModelAndView toWelcome(HttpServletRequest request){
    	ModelAndView model = new ModelAndView();
    	model.setViewName("welcome");
        return model;
    }
}
